/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.himilco.ocana.workflow.core.dao;

import com.himilco.ocana.workflow.core.dao.entities.Incident;
import com.himilco.ocana.workflow.core.dao.entities.Ticket;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author oussema
 */
public class SupervisorDAL {

    private EntityManager em;

    public SupervisorDAL(EntityManager entityManager) {
        this.em = entityManager;
    }

    public SupervisorDAL() {
    }

    public List<Ticket> findAll() {
        initDb();
        update();
        javax.persistence.Query query = em.createQuery("SELECT i FROM db_ocana_ticket i where status='ATTENDING_SITE'");
        List<Ticket> results = query.getResultList();
        System.out.println("Nbr Tickets DAL  confirme: " + results.size());
        return results;
    }

    public void initDb() {
        Ticket ticket1 = new Ticket();
        Incident incident1 = new Incident("CASH PROBLEM", "IMPORTANT", "IMPORTANT", "IMPORTANT", "1H", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT");
        ticket1.setStartTime(new Date());
        ticket1.setStatus("OPEN");
        ticket1.setIncident(incident1);
        em.persist(incident1);
        em.persist(ticket1);

        Ticket ticket2 = new Ticket();
        Incident incident2 = new Incident("KEY BOARD PROBLEM", "IMPORTANT", "TOTO", "TOTO", "30MN", "TOTO", "TOTO", "TOTO", "TOTO", "TOTO", "TOTO", "TOTO", "TOTO", "TOTO");
        ticket2.setStartTime(new Date());
        ticket2.setStatus("MANUAL_INTERVENTION");
        ticket2.setIncident(incident2);


        em.persist(incident2);
        em.persist(ticket2);
        Ticket ticket3 = new Ticket();
        ticket3.setStatus("SERVICE_NOTIFIED");
        em.persist(ticket3);
        Ticket ticket4 = new Ticket();
        ticket4.setStatus("CONFIRMED_INCIDENT");
        em.persist(ticket4);
        Ticket ticket5 = new Ticket();
        ticket5.setStatus("ATTENDING_SITE");
        em.persist(ticket5);
    }

    public void updateTicket(Ticket t) {
        em.merge(t);
    }

    public void update() {
//        Query q =em.createQuery("UPDATE Ticket t SET t.status='ATTENDING_SITE' WHERE t.status LIKE :status");
//        q.setParameter("status", "CONFIRMED_INCIDENT");
        Query query = em.createQuery("SELECT i FROM db_ocana_ticket i where status='CONFIRMED_INCIDENT'");
        List l = new ArrayList<Ticket>();
        l = query.getResultList();
        for (Iterator it = l.iterator(); it.hasNext();) {
            Ticket t = (Ticket) it.next();
            t.setStatus("ATTENDING_SITE");
            em.merge(t);
        }

    }
//    public void updateToConfirmedTicket() {
//        
//    }
//    public List<Ticket> findAll() {
////        initDb();
//        javax.persistence.Query query = em.createQuery("SELECT i FROM db_ocana_ticket i where status='CONFIRMED_INCIDENT'");
//        List<Ticket> results = query.getResultList();
////        System.out.println("Nbr Tickets DAL  confirme: " + results.size());
//        return results;
//
//    }
//    public List<Ticket> findAll() {
////        initDb();
//        javax.persistence.Query query = em.createQuery("SELECT i FROM db_ocana_ticket i where status='OPEN'"); 
//        List<Ticket> results = query.getResultList();
//        System.out.println("Nbr Tickets BO: " + results.size());
//        return results;
//    }    
//    public void initDb() {
//        Ticket ticket1 = new Ticket();
//        Incident incident1 = new Incident("CASH PROBLEM", "IMPORTANT", "IMPORTANT", "IMPORTANT", "1H", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT");
//        ticket1.setStartTime(new Date());
//        ticket1.setStatus("CONFIRMED_INCIDENT");
//        ticket1.setIncident(incident1);
//        em.persist(incident1);
//        em.persist(ticket1);
//    }
}
